welcome to another lesson data discretization and many times we are going to again reduce our data and we learned how to do the dimension elimination we learned how to do sampling they're working on the number of columns and the number of rows sometimes we just want to work on the actual value of those attributes for example if we have a continuously numeric attribute and they just cover infinite number of possible value we may want to make it simple as kind of intervals because those intervals will be represented by certain labels and for example we can reduce the age which will be 0-120 or 130 as a 100 more labels but if we can convert that continuously numeric attributes into certain labels into certain categories such as baby such as teenagers such as adults such as senior then we only have 4 or 5 labels for us to work with and we don't have to care about if the person is 29 or 30 is because they are in the same interval that will significantly reduce our data as well so the discreditization is again one method of reduced the data and sometimes the discretization can also make the data more meaningful as I said because sometimes we don't care about a minor difference between the numeric continuously value we just want to put them into different categories and then work with those categories let's get into the setup we are going to use our Numpy as np we are going to use our pandas as pd again we are going to to use our California Housing Train CSV which is built in again you can click the file you can go to the sample data you can go to this California Housing Train click that path and the paste into here and then you will have the database loaded and we have visited this database multiple times so we have the longitude latitude and so on and so forth and most of the attributes are numeric and then we are going to work on the particular value of them and try to make them a little bit more easy for us to manage and for us to do analysis the df.describe() is a good way to show the statistic understanding of them it is also a good way for us to do discretization is it because when you do the discretization for intervals you do want to have them kind of balanced in order to find that balance in order to make it meaningful this describe can help us a lot is because we know the describe has our Q1 Q2 and Q3 so what is exactly Q1? Q1 is actually the threshold for you if you want to know what is your first quantile? that is what is your first 25% of your data if you sort them from the least to the greatest and the 50 basically means that what is your first behalf of your data if you sort them from the least to the greatest and again 75 basically will be your 75% quantile and represents your 3/4 of your data that if you sorted them well so these Q1, Q2, and Q3 naturally can help you to do a discreditation if you want to do 2 categories which with almost the equal number of records then Q2 will be your choice if you want to do the discretization for 4 categories then Q1 Q2 and Q3 can be the separating point or a splitting point for you to separate your records into 4 different bins of course if you want to do 3 bins or other bins you can always call the quantile() specify that particular value and then use the value for your thresholds again we just learned how to do discretization using quantiles because quantiles can make a balanced sub data set or balance the category in terms of the number of records if you want to do the category or discretization intervals based on the value then you can go to the mean because the mean basically gives you the average of your data if you use the mean as a threshold to cut the category or split your data set then you can get one category as every record less than the mean and another category in which every record higher than min and if you want to use the mean and standard deviation then you can get the categories such as super common in your data set that will might be mean +/- std if you want to get your category as really rare and uncommon then you may have the mean +/- 3 * std or 2 * std basically the mean and standard deviation can help you to find a certain threshold okay so let's look into let's look into this quite high population as the example in here we are going to use the mean and let's check the population and the mean is around 1400 I just copied it to this place so we are going to use a new column and this column is called popular and this popular column is actually based on the population and we are going to use a numpy built-in method called select(), basically we are going to select this kind of condition and if this condition is true we are going to select not popular if this condition is true we are going to select popular so basically the Numpy select will prepare 2 things the first one is the condition list the second one is the result list and the result will be based on your condition given to the select so let's print out this df['popular'] and of course because we have only 2 labels the df['popular'] will be a categorical and the dtype will be object so because it is categorical let's just do the value_counts we can see here not popular is around 10,000 and popular is around 6000 okay so this will be this discretize population example let's look at our rooms for the rooms we want to have a little bit more complex condition rather than just the two conditions we can have them into for example 4 different conditions so in the conditions we can define a list and this list is actually a list of more compound conditions so this is the total room will be less than 1462 and the total bedroom will also be less than 297 I believe I choose this one 1462 for the total of the room is the Q1 and the total bedroom is also Q1 so that is my criteria of choosing the thresholds and we can set up other conditions that this is more than Q3 this is more than Q3 and this is less than Q2 but this is more than Q2 and this is more than Q2 and this is less than Q2 so of course these conditions you can name as many as you want as long as you can provide the same number of the possible labels or possible outcome for those conditions if they are satisfied and in this example of course my conditions are not fully complete of this 100% possibility that is the reason you will see in the result as the df['rooms'] we have HH and LL we have HL and we have a lot of 0s those 0s will be the default value if none of this condition is satisfied then let's do the value_counts() again so as you can see we have all low as 3424, all high or we have the high low and we have low high and then basically we have all the other 8,000 as 0 which is not covered in my condition which is indicating either I have to go into the condition and make them more inclusive or I have to label this 0 as something else let's look into the house value and use our last example in this we are not going to use the numpy and the select because sometimes it will be hard to think about those conditions and then make the values as the label in those functions so rather than doing the numpy select we can actually use another very handy function built-in in Dataframe that is the apply() so in order to use apply() we first define a function of course you can use the np.select to do the same thing but here I just want to show you we have another alternative choice so the define house value with a certain value fit in is actually quite simple based on the certain value we are going to discretize the value of the house into low and high and the medium and once we have this very simple definition of the function defined in this place you can see we can apply this function to all the value of the median house value that will be the element wise application of this function so you can think about this as a shortcut of applying any and each of the row or the cell value in this df['median_house_value'] as the argument passed to house value right here so the house value category will be based on the returned value in your defined function house value that is why our house value category will be looking like this we can do another value counts which we have around 8500 of the medium and we have 4000 and 4000 of the high and the low and why we can see it is basically equally distributed is because these two again, this one is Q1 and this one is Q3 so everything in between should be 50% of your data set so that is why again the describe() help you to do to do discretization and I hope you like this tutorial and I will see you later on